package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.BsbRapidAnswerMapper;
import com.bsb.base.mapper.BsbUserMapper;
import com.bsb.base.model.AnswerUser;
import com.bsb.base.model.BsbRapidAnswer;
import com.bsb.base.model.BsbRapidAnswerExample;
import com.bsb.base.model.BsbRapidAnswerExample.Criteria;
import com.bsb.base.model.BsbUser;

@Service
public class BsbRapidAnswerService{
	
	@Resource
	private BsbRapidAnswerMapper bsbRapidAnswerMapper;
	@Resource
	private BsbUserMapper bsbUserMapper;
	
	public void addBsbRapidAnswer(BsbRapidAnswer bsbRapidAnswer) {
		bsbRapidAnswerMapper.insertSelective(bsbRapidAnswer);
	}
	
	public void delBsbRapidAnswerByUuid(String uuid) {
		bsbRapidAnswerMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateBsbRapidAnswer(BsbRapidAnswer bsbRapidAnswer) {
		bsbRapidAnswerMapper.updateByPrimaryKeySelective(bsbRapidAnswer);
	}
	
	public void updateBsbRapidAnswerByExample(BsbRapidAnswer bsbRapidAnswer,BsbRapidAnswerExample bsbRapidAnswerExample){
	    bsbRapidAnswerMapper.updateByExampleSelective(bsbRapidAnswer,bsbRapidAnswerExample);
	}
	
	public BsbRapidAnswer getBsbRapidAnswerByUuid(String uuid) {
		return bsbRapidAnswerMapper.selectByPrimaryKey(uuid);
	}
	
	public List<BsbRapidAnswer> getPageBsbRapidAnswer(BsbRapidAnswerExample bsbRapidAnswerExample) {
		return bsbRapidAnswerMapper.selectPage(bsbRapidAnswerExample);
	}
	
	public int countBsbRapidAnswer(BsbRapidAnswerExample bsbRapidAnswerExample) {
		return bsbRapidAnswerMapper.countByExample(bsbRapidAnswerExample);
	}
	
	public List<BsbRapidAnswer> selectByExample(BsbRapidAnswerExample example){
	    return bsbRapidAnswerMapper.selectByExample(example);
	}
	/**
	 * 根据问题id获取悬赏回答
	 * @param rid
	 * @return
	 */
	public List<BsbRapidAnswer> getAnswers(String rid){
		if(StringUtils.isEmpty(rid)){
			return null;
		}
		BsbRapidAnswerExample example = new BsbRapidAnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andRapidIdEqualTo(rid);
		List<BsbRapidAnswer> answers = bsbRapidAnswerMapper.selectByExample(example );
		return answers;
	}
	
	public List<AnswerUser> getAnswerUsers(String rid){
		List<BsbRapidAnswer> answers = getAnswers(rid);
		List<AnswerUser> answerusers = new ArrayList<AnswerUser>();
		for (BsbRapidAnswer bsbRapidAnswer : answers) {
			AnswerUser ansuser = new AnswerUser();
			String userId = bsbRapidAnswer.getUserId();
			BsbUser user = bsbUserMapper.selectByPrimaryKey(userId);
			if(null != user){
				ansuser.setCusername(user.getName());
			}
			ansuser.setCuserid(userId);
			ansuser.setAnswerDate(bsbRapidAnswer.getAnsDate());
			answerusers.add(ansuser);
		}
		return answerusers;
	}

	public BsbRapidAnswer getadoptUser(String rid) {
		if(StringUtils.isEmpty(rid)){
			return null;
		}
		BsbRapidAnswerExample example = new BsbRapidAnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andRapidIdEqualTo(rid);
		criteria.andStatusEqualTo("1");
		List<BsbRapidAnswer> answers = bsbRapidAnswerMapper.selectByExample(example );
		if(null != answers && answers.size()>0){
			return answers.get(0);
		}
		return new BsbRapidAnswer();
	}
}
